<?php

namespace App\Services\Formatter;

use App\Model\Meeting;
use Carbon\Carbon;
use Hyperf\Utils\Codec\Json;

class MeetingFormatter
{
    public function stdoutFormatter(Meeting $model): array
    {
        return [
            'id' => $model->id,
            'userId' => $model->user_id,
            'title' => $model->title,
            'content' => $model->content,
            'userLimit' =>$model->user_limit,
            'signRules' => Json::decode($model->sign_rules),
            'signFields' => $model->sign_fields==''?'':Json::decode($model->sign_fields),
            'signType' => $model->sign_type,
            'allowLaterSign' => $model->allow_later_sign,
            'isPublish' => $model->is_publish,
            'status' => $model->status,
            'startTime' => Carbon::parse($model->start_time)->format('Y-m-d H:i:s'),
            'endTime' => Carbon::parse($model->end_time)->format('Y-m-d H:i:s'),
            'signInStart' => $model->sign_in_start ? Carbon::parse($model->sign_in_start)->format('Y-m-d H:i:s') : null,
            'signInEnd' => $model->sign_in_end ? Carbon::parse($model->sign_in_end)->format('Y-m-d H:i:s') : null,
            'signOutStart' => $model->sign_out_start ? Carbon::parse($model->sign_out_start)->format('Y-m-d H:i:s') : null,
            'signOutEnd' => $model->sign_out_end ? Carbon::parse($model->sign_out_end)->format('Y-m-d H:i:s') : null,
            'supplementaryStart' => $model->supplementary_start ? Carbon::parse($model->supplementary_start)->format('Y-m-d H:i:s') : null,
            'supplementaryEnd' => $model->supplementary_end ? Carbon::parse($model->supplementary_end)->format('Y-m-d H:i:s') : null,
            'createTime' => Carbon::parse($model->created_at)->format('Y-m-d H:i:s'),
            'updateTime' => Carbon::parse($model->updated_at)->format('Y-m-d H:i:s'),
        ];
    }

    public function formatList($models): array
    {
        $result = [];
        foreach ($models as $model) {
            /* @var $model Meeting */
            $result[] = $this->stdoutFormatter($model);
        }
        return $result;
    }
}
